/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.click.app.webapp.action;

import com.click.app.model.Phone;
import com.click.app.model.Supplier;
import com.click.app.service.PhoneManager;
import com.click.app.service.SupplierManager;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author ariel
 */
public class SupplierAction extends BaseAction {

    private SupplierManager supplierManager;
    private PhoneManager phoneManager;
    private List<Supplier> suppliers;
    private Supplier supplier;
    private Long id;
    private List<Phone> phoneList;
    private Phone phone;
    private Integer phoneIndex;

    public void setId(Long id) {
        this.id = id;
    }

    public Supplier getSupplier() {
        return supplier;
    }

    public void setSupplier(Supplier supplier) {
        this.supplier = supplier;
    }

    public Phone getPhone() {
        return phone;
    }

    public void setPhone(Phone phone) {
        this.phone = phone;
    }

    public List<Phone> getPhoneList() {
        return phoneList;
    }

    public Integer getPhoneIndex() {
        return phoneIndex;
    }

    public void setPhoneIndex(Integer phoneIndex) {
        this.phoneIndex = phoneIndex;
    }

    public String refreshPhoneList() {
        System.out.println("refreshing phones...");
        phoneList = (List<Phone>) getSession().getAttribute("phoneList");

        return SUCCESS;
    }

    public String deletePhone() {
        System.out.println("deleting Phone: " + phoneIndex + "...");

        phoneList = (List<Phone>) getSession().getAttribute("phoneList");

        phone = phoneList.get(phoneIndex);

        if (phone.getId() != null) {
            List<Phone> deletePhoneList = (List<Phone>) getSession().getAttribute("deletePhoneList");
            deletePhoneList.add(phone);
            getSession().setAttribute("deletePhoneList", deletePhoneList);
        }
        
        phoneList.remove(phone);

        getSession().setAttribute("phoneList", phoneList);

        phone = new Phone();
        phoneIndex = null;

        return SUCCESS;
    }

    public String savePhone() {

        System.out.println("saving Phone...");

        phoneList = (List<Phone>) getSession().getAttribute("phoneList");

        if (phoneIndex == null) {
            phoneList.add(phone);
        } else {
            phoneList.set(phoneIndex, phone);
            phoneIndex = null;
        }

        getSession().setAttribute("phoneList", phoneList);

        phone = new Phone();

        return SUCCESS;
    }

    public String delete() {
        System.out.println("deleting Supplier...");

        supplierManager.remove(supplier.getId());
        saveMessage(getText("supplier.deleted"));

        getSession().removeAttribute("phoneList");
        getSession().removeAttribute("deletePhoneList");

        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            supplier = supplierManager.get(id);
            phoneList = phoneManager.findByPerson(supplier);
            phone = new Phone();
            getSession().setAttribute("phoneList", phoneList);
            getSession().setAttribute("deletePhoneList", new ArrayList<Phone>());
        } else {
            supplier = new Supplier();
            phoneList = new ArrayList<Phone>();
            phone = new Phone();
            getSession().setAttribute("phoneList", phoneList);
            getSession().setAttribute("deletePhoneList", new ArrayList<Phone>());
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        System.out.println("saving Supplier...");

        boolean isNew = (supplier.getId() == null);

        List<Phone> deletePhoneList = (List<Phone>) getSession().getAttribute("deletePhoneList");
        for (Phone p : deletePhoneList) {
            phoneManager.remove(p.getId());
        }

        supplier = supplierManager.save(supplier);

        phoneList = (List<Phone>) getSession().getAttribute("phoneList");
        for (Phone p : phoneList) {
            if (p.getId() == null) {
                p.setPerson(supplier);
            }

            phoneManager.save(p);
        }

        String key = (isNew) ? "supplier.added" : "supplier.updated";
        saveMessage(getText(key));

        getSession().removeAttribute("phoneList");
        getSession().removeAttribute("deletePhoneList");

        return SUCCESS;
    }

    @Override
    public String cancel() {
        getSession().removeAttribute("phoneList");
        getSession().removeAttribute("deletePhoneList");

        return CANCEL;
    }

    public void setSupplierManager(SupplierManager supplierManager) {
        this.supplierManager = supplierManager;
    }

    public void setPhoneManager(PhoneManager phoneManager) {
        this.phoneManager = phoneManager;
    }

    public List<Supplier> getSuppliers() {
        return suppliers;
    }

    public String list() {
        suppliers = supplierManager.getAll();
        return SUCCESS;
    }

    public String help(){
        return SUCCESS;
    }

    public String helpList(){
        return SUCCESS;
    }
}
